from paramiko import SSHClient, AutoAddPolicy
from scp import SCPClient

from src.bean.config_column_enum import ConfigColumnEnum
from src.util.common_util import input_is_yes
from src.util.file_selector_util import select_multi_file


def scp_entrance(config):
    file_path_list = select_multi_file(config.get_config(ConfigColumnEnum.use_terminal.value),
                                       title='请选择要传输的文件')
    if input_is_yes("是否传到/home/supervisor/?"):
        destination = '/home/supervisor/'
    else:
        destination = input("请输入目标位置:")
    scp_upload(
        host='10.28.1.140',
        port=22,
        username='supervisor',
        password='Avic@2019',
        local_path=file_path_list,
        remote_path=destination
    )


def progress(filename, size, sent):
    percent = float(sent) / float(size) * 100
    print(f"\r传输中: {filename} {percent:.2f}% ({sent}/{size} bytes)", end='')


def scp_upload(host, port, username, password, local_path, remote_path):
    ssh = SSHClient()
    ssh.set_missing_host_key_policy(AutoAddPolicy())
    ssh.connect(host, port=port, username=username, password=password)
    with SCPClient(ssh.get_transport(), progress=progress) as scp:
        scp.put(local_path, remote_path, recursive=True)
    ssh.close()
